package com.situ.firmadmin.service.impl;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.situ.firmadmin.constants.RedisConstants;
import com.situ.firmadmin.listener.UserExcelListener;
import com.situ.firmadmin.mapper.AdminMapper;
import com.situ.firmadmin.mapper.DeptMapper;
import com.situ.firmadmin.mapper.RoleTypeMapper;
import com.situ.firmadmin.mapper.UserMapper;
import com.situ.firmadmin.pojo.User;
import com.situ.firmadmin.pojo.query.UserQuery;
import com.situ.firmadmin.pojo.vo.TypeCountVO;
import com.situ.firmadmin.pojo.vo.UserDeptVO;
import com.situ.firmadmin.service.IUserService;
import com.situ.firmadmin.util.ExcelUtil;
import com.situ.firmadmin.util.PageResult;
import com.situ.firmadmin.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AdminMapper adminMapper;
	@Autowired
	private DeptMapper deptMapper;
	@Autowired
	private RoleTypeMapper roleTypeMapper;
    
    @Override
    public User login1(String name, String password) {
        System.out.println("name:" + name + ",password:" + password);
        try{
            return userMapper.login1(name,password);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public User login(String name) {
        return userMapper.login(name);
    }
    
    @Override
    public PageResult selectByPage(UserQuery userQuery,User user) {
        PageHelper.startPage(userQuery.getPage(), userQuery.getLimit());
        List<UserDeptVO> list=userMapper.selectByPage(userQuery,user);
        PageInfo<UserDeptVO> pageInfo=new PageInfo<>(list);
        int total=(int)pageInfo.getTotal();
        return new PageResult(0,"",total,list);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void insert(User user) {
        userMapper.insert(user);
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, user.getImage());
        adminMapper.deleteAdmin();
        adminMapper.updateAdmin();
    }

    @Override
    public void deleteById(int id) {
        userMapper.deleteById(id);
        adminMapper.deleteAdmin();
        adminMapper.updateAdmin();
    }

    @Override
    public void deleteAll(Integer[] ids) {
        userMapper.deleteAll(ids);
        adminMapper.deleteAdmin();
        adminMapper.updateAdmin();
    }

    @Override
    public User selectById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, user.getImage());
        adminMapper.deleteAdmin();
        adminMapper.updateAdmin();
    }

    @Override
    public List<TypeCountVO> echarts() {
        return userMapper.echarts();
    }
    
    @Override
    public User getOne(String name) {
        return userMapper.getOne(name);
    }
    
    @Override
    public User getUser() {
        Map<String, Object> map1 = ThreadLocalUtil.get();
        Integer id = (Integer) map1.get("id");
        return userMapper.selectById(id);
    }
    
    @Override
    public void importExcel(MultipartFile file) {
		try {
			EasyExcel.read(file.getInputStream(),UserDeptVO.class,new UserExcelListener(userMapper,deptMapper,roleTypeMapper)).sheet().doRead();
            adminMapper.deleteAdmin();
            adminMapper.updateAdmin();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
    
    @Override
    public void exportExcel(HttpServletResponse response) {
        List<UserDeptVO> list=userMapper.selectAll();
        ExcelUtil.exportExcel(response,list,UserDeptVO.class,"用户");
    }
    
    @Override
    public List<TypeCountVO> selectDeptCount() {
        return userMapper.selectDeptCount();
    }
    
    @Override
    public Long selectCount() {
        return userMapper.selectCount();
    }
    
    @Override
    public void resetPassword(Integer id,String password) {
        userMapper.resetPassword(id,password);
    }
    
}
